<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
require_once($phpbb_root_path . 'sv_common.' . $phpEx);
require_once($phpbb_root_path . 'fb/facebook.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('stockvirtual');

$money = $user->data['user_money'];
$user_id 	= $user->data['user_id'];

if ($user_id != ANONYMOUS)
{
	// list all holding stock.
	list_holding_stock();

	// list all open to_buy delegation. 
	list_open_to_buy_delegation();

	// list all open to_sell delegation. 
	list_open_to_sell_delegation();
	
	// list all related equity change events.
	list_equity_change_event($user_id);
}
else
{
	// Create our Application instance (replace this with your appId and secret).
	$facebook = new Facebook(array(
		'appId'  => get_fb_app_id(),
  		'secret' => get_fb_app_secret(),
  		'cookie' => true,
	));
	
	$session = $facebook->getSession();

	$me = null;
	// Session based API call.
	if ($session) 
	{
  		try 
  		{
    		$uid = $facebook->getUser();
    		$me = $facebook->api('/me');
  		} 
  		catch (FacebookApiException $e) 
  		{
    		error_log($e);
  		}
	}

	if ($me) 
	{
  		$logoutUrl = '';
  		$fb_id = $uid;
  		
  		// get the user name from FB id. 
  		$sql 	= 'SELECT * FROM phpbb_facebook_user WHERE fb_id=' . $fb_id;
  		$result	= $db->sql_query_limit($sql, 1);
  		if ($row = $db->sql_fetchrow($result))
  		{
	  		// if user exist, login.
	  		$user_id = $row['user_id'];  			
  			$user->session_create($user_id, false, true);
		}
  		else
  		{
	  		// or create the user and insert FB info.
	  		$username = $me['name'];			
			$password = $uid;
			
			if (username_exist($username))
			{
				$username = $username . '(Facebook)';	
			}
			
	  		// Setup User Data
			$user_row = array(
		    	'username' => $username,
			    'user_password' => phpbb_hash($password),
			    'user_email' => $me['link'],
			    'group_id' => 2,
		    	'user_timezone' => '8',
			    'user_dst' => '0',
			    'user_lang' => $config['default_lang'],
			    'user_type' => '0',
		    	'user_actkey' => '',
			    'user_ip' => $_SERVER['REMOTE_ADDR'],
			    'user_inactive_reason' => '0',
			    'user_website' => $me['link'],
		    	'user_inactive_time' => '0');
			$user_id = user_add($user_row);
		
			$user->session_create($user_id, false, true);
			
			$sql = 'INSERT INTO phpbb_facebook_user (fb_id, user_id, name, first_name, last_name, link, gender, timezone, locale, verified) VALUES (' . $me['id'] . ', ' . $user_id . ', "' . $me['name'] . '", "' . $me['first_name'] . '", "' . $me['last_name'] . '", "' . $me['link'] . '", "' . $me['gender'] . '", "' . $me['timezone'] . '", "' . $me['locale'] . '", "' . $me['verified'] . '")';
			$db->sql_query($sql);
		}
		
		// refresh
		$url = append_sid("{$phpbb_root_path}index.$phpEx", '');
		redirect($url, false, true);
		echo "<a href='" . $url . "'>Redirecting...</a><br /><br />" . $url;
	} 
	else 
	{
  		$loginUrl = $facebook->getLoginUrl();
	}
	

	// show ranking directly on index page if the user is not logged in.
	// list highest 20 user values in the latest date. 
	$show_user_value = list_user_value_ranking(20);

	// list highest 20 user ROI in 1 month period. 
	$show_1_month_roi = list_user_roi_ranking(30, 'roi_1_month_list', 20);

}

if (is_newbie($user_id))
{
	$show_welcome_newbie	= true;
	$show_rule 				= true;	
}

$show_statistics = false;
if ($auth->acl_getf_global('m_'))
{
	$show_statistics = true;
}
$show_adm_query = false;
if ($auth->acl_getf_global('m_'))
{
	$show_adm_query = true;
}
$show_adm_equity_change = false;
if ($auth->acl_getf_global('m_'))
{
	$show_adm_equity_change = true;
}

$is_following_anyone = is_following_anyone($user_id);

display_forums('', $config['load_moderators']);



// Assign index specific vars
$template->assign_vars(array(

	'FORUM_IMG'				=> $user->img('forum_read', 'NO_NEW_POSTS'),
	'FORUM_NEW_IMG'			=> $user->img('forum_unread', 'NEW_POSTS'),
	'FORUM_LOCKED_IMG'		=> $user->img('forum_read_locked', 'NO_NEW_POSTS_LOCKED'),
	'FORUM_NEW_LOCKED_IMG'	=> $user->img('forum_unread_locked', 'NO_NEW_POSTS_LOCKED'),

	'S_LOGIN_ACTION'			=> append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'),
	'S_BUY_ACTION'				=> append_sid("{$phpbb_root_path}buy.$phpEx", ''),
	'S_SELL_ACTION'				=> append_sid("{$phpbb_root_path}sell.$phpEx", ''),
	'S_PERFORMANCE_ACTION'		=> append_sid("{$phpbb_root_path}performance.$phpEx", ''),
	'S_HISTORY_ACTION'			=> append_sid("{$phpbb_root_path}history.$phpEx", ''),
	'S_RANKING_ACTION'			=> append_sid("{$phpbb_root_path}ranking.$phpEx", ''),
	'S_RULE'					=> append_sid("{$phpbb_root_path}rule.$phpEx", ''),
	'S_STATISTICS'				=> append_sid("{$phpbb_root_path}statistics.$phpEx", ''),
	'S_ADM_QUERY'				=> append_sid("{$phpbb_root_path}adm_query.$phpEx", ''),
	'S_ADM_EQUITY_CHANGE'		=> append_sid("{$phpbb_root_path}adm_equity_change.$phpEx", ''),
	'S_QUERY_USER'				=> append_sid("{$phpbb_root_path}query_user.$phpEx", 'mode=query_user'),
	'S_FOLLOW_INFO'				=> append_sid("{$phpbb_root_path}follow_info.$phpEx", ''),
	
	'S_FB_LOGIN_URL'			=> $loginUrl,
	
	'S_DISPLAY_BIRTHDAY_LIST'	=> ($config['load_birthdays']) ? true : false,

	'S_TO_BUY_DELEGATION_LIST'	=> $to_buy_delegation_list,
	'S_TO_SELL_DELEGATION_LIST'	=> $to_sell_delegation_list,
	'S_HOLDING_STOCK_LIST'		=> $holding_stock_list,
	'S_EQUITY_CHANGE_EVENT_LIST'=> $equity_change_event_list,
	
	'U_MONEY'					=> number_format($money),
	'S_HIDE_NAV_LINK'			=> true,
	'S_SHOW_RULE'				=> $show_rule,
	'S_SHOW_WELCOME_NEWBIE'		=> $show_welcome_newbie,
	'S_SHOW_STATISTICS'			=> $show_statistics,
	'S_SHOW_ADM_QUERY'			=> $show_adm_query,
	'S_SHOW_ADM_EQUITY_CHANGE'	=> $show_adm_equity_change,
	
	'U_MARK_FORUMS'		=> ($user->data['is_registered'] || $config['load_anon_lastread']) ? append_sid("{$phpbb_root_path}index.$phpEx", 'hash=' . generate_link_hash('global') . '&amp;mark=forums') : '',
	'U_MCP'				=> ($auth->acl_get('m_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=front', true, $user->session_id) : '',

	'SHOW_USER_VALUE'	=>	$show_user_value,
	'SHOW_1_MONTH_ROI'	=>	$show_1_month_roi,
	
	'IS_FOLLOWING_ANYONE'	=>	$is_following_anyone,
	
));

// Output page
page_header($user->lang['INDEX']);

$template->set_filenames(array(
	'body' => 'index_body.html')
);

page_footer();

?>